﻿using System;
using System.Collections.Generic;

namespace RedisDotNet
{
    /// <summary>
    /// DBC assertions
    /// </summary>
    public static class Check
    {
        private static Func<string, bool> isKeyFormatOk = 
            key => key.IndexOfAny(new[] { '\n', '\r', ' ' }) < 0;

        private static Func<string, bool> isKeyLengthOk = 
            key => key.Length > 0;

        private static Func<string, bool> isKeyOk = 
            key => isKeyFormatOk(key) && isKeyLengthOk(key);

        public static void IsValidKey(string key)
        {
            if (!isKeyOk(key))
                throw new RedisClientException("Invalid key");
        }

        public static void IsValidKey(List<KeyValuePair<string, string>> data)
        {
            foreach (var pair in data)
            {
                Check.IsValidKey(pair.Key);
                Check.IsNotNullOrEmpty(pair.Value, "data");
            }
        }

        public static void IsValidKey(string[] keys)
        {
            foreach (var key in keys)
                Check.IsValidKey(key);
        }

        public static void IsInRange(int value, int low, int high, string parameter)
        {
            if (value < low || value > high)
                throw new ArgumentOutOfRangeException(parameter);
        }

        public static void IsNotNullOrEmpty(string value, string parameter)
        {
            if (string.IsNullOrEmpty(value))
                throw new ArgumentException("String empty or null", parameter);
        }

        public static void IsNotNull(object data, string p)
        {
            if (data == null)
                throw new ArgumentNullException(p);
        }
    }
}
